<?php
namespace app\home\logic;

use app\common\logic\Base;
use think\facade\App;
use think\facade\Request;

class UrlGroupRelation extends  Base {
    
    /**
     * @desc 我的链接
     * @param array $params
     * @param int $userId
     * @return array
     * @throws \Exception
     */
    public function index($params, $userId) {
        $urlGroupRelationWhere = [
            ['ugr_user_id', '=', $userId],
            ['ugr_group_id', '=', $params['id']]
        ];

        $list = model('UrlGroupRelation')
            ->where($urlGroupRelationWhere)
            ->leftJoin('Url', 'url_id = ugr_url_id')
            ->leftJoin('UrlGroup', 'ug_group_id = ugr_group_id')
            ->limit(10)
            ->select();

        //分组列表
        $groupWhere = [
            ['ug_user_id', '=', $userId],
            ['ug_group_id', '=', $params['id']]
        ];
        //分组信息
        $groupInfo = model('UrlGroup')
            ->field('ug_group_id AS groupId, ug_group_name AS groupName')
            ->where($groupWhere)
            ->find();

        //查询短链接信息,筛选已添加到分组里面的
        $urlWhere[] = ['user_id', '=', $userId];
        $urlIds = model('UrlGroupRelation')
            ->where($urlGroupRelationWhere)
            ->select()
            ->column('ugr_url_id');
        if($urlIds) {
            $urlWhere[] = ['url_id', 'not in', $urlIds];
        }
        $urlList = model('Url')
            ->field('url_id,keyword,link')
            ->where($urlWhere)
            ->limit(100)
            ->order('url_id', 'desc')
            ->select();

        //短链接地址
        $shortUrl = model('Domain')->getDomain($userId);

        return [
            'list'       => $list,
            'urlList'    => $urlList ? output_format($urlList) : [] ,
            'params'     => $params,
            'shortUrl'   => $shortUrl,
            'groupInfo'  => $groupInfo
        ];
    }

    /**
     * @desc 添加分组关联
     * @param array $params
     * @param int $userId
     * @return bool
     * @throws \Exception
     */
    public function add($params, $userId) {
        $where = [
            ['ugr_user_id', '=', $userId],
            ['ugr_url_id', 'in', $params['urlId']],
            ['ugr_group_id', '=', $params['groupId']]
        ];
        $urlGroupRelation = model('UrlGroupRelation')
            ->where($where)
            ->find();
        if($urlGroupRelation) {
            $this->errorCode = EC_HOME_URL_GROUP_RELATION_ALREADY_EXIST;
            return  false;
        }

        $insertData = [];
        foreach ($params['urlId'] as $v) {
            $insertData[] = [
                'ugr_url_id'      => $v,
                'ugr_user_id'     => $userId,
                'ugr_group_id'    => $params['groupId'],
                'ugr_create_time' => date('Y-m-d H:i:s')
            ];
        }

        $result = model('UrlGroupRelation')->insertAll($insertData);
        if($result === false) {
            $this->errorCode = EC_DATABASE_ERROR;
            return false;
        }
        return true;
    }

    /**
     * @desc 删除分组关联
     * @param int $id
     * @param int $userId
     * @return bool
     * @throws \Exception
     */
    public function del($id, $userId) {
        $where = [
            ['ugr_id', '=', $id],
            ['ugr_user_id', '=', $userId]
        ];
        $urlGroupRelation = model('UrlGroupRelation')
            ->where($where)
            ->find();
        if(!$urlGroupRelation) {
            $this->errorCode = EC_HOME_URL_GROUP_RELATION_NOT_EXIST;
            return  false;
        }

        $result = model('UrlGroupRelation')
            ->where($where)
            ->delete();

        if($result === false) {
            $this->errorCode = EC_DATABASE_ERROR;
            return false;
        }
        return true;
    }
}
